using System;
using System.Web.UI;
using TTQT.TTQT_Code;

namespace TTQT
{
	/// <summary>
	/// Summary description for MasterPage.
	/// </summary>
	public class MasterPage:Page
	{
		public string brnCode
		{
			get
			{
				if(null !=Session["Brn_Code"])
				{
					return Session["Brn_Code"].ToString();
				}
				else
				{
					return string.Empty;
				}
			}
		}
		public string userId
		{
			get
			{
				if(null !=Session["UserId"])
				{
					return Session["UserId"].ToString();
				}
				else
				{
					return string.Empty;
				}
			}
		}
		public string userName
		{
			get
			{
				if(null !=Session["UserName"])
				{
					return Session["UserName"].ToString();
				}
				else
				{
					return string.Empty;
				}
			}
		}

		public string isSuperAdmin
		{
			get
			{
				if(null !=Session["IsSuperAdmin"])
				{
					return Session["IsSuperAdmin"].ToString();
				}
				else
				{
					return string.Empty;
				}
			}
		}

		public bool isCNN
		{
			get
			{
				if(null!=Session["CNN"])
				{
					if(Session["CNN"].ToString().Equals("0"))
					{
						return false;
					}
					else if(Session["CNN"].ToString().Equals("1"))
					{
						return true;
					}
				}
				else
				{
					return false;
				}
				return false;
			}
		}

		public bool isTTQT
		{
			get
			{
				if(null!=Session["TTQT"])
				{
					if(Session["TTQT"].ToString().Equals("0"))
					{
						return false;
					}
					else if(Session["TTQT"].ToString().Equals("1"))
					{
						return true;
					}
				}
				else
				{
					return false;
				}
				return false;
			}
		}
		public bool isBranchManager
		{
			get 
			{
				if(null!=Session["GroupId"])
				{
					if(Session["GroupId"].ToString().Equals("1"))
					{
						return true;
					}
				}
				return false;
			}
		}
		public bool isKSV
		{
			get 
			{
				if(null!=Session["GroupId"])
				{
					if(Session["GroupId"].ToString().Equals("2"))
					{
						return true;
					}
				}
				return false;
			}
		}
		public bool isTTV
		{
			get 
			{
				if(null!=Session["GroupId"])
				{
					if(Session["GroupId"].ToString().Equals("3"))
					{
						return true;
					}
				}
				return false;
			}
		}
		public bool isRole(string group_name)
		{
			try
			{
				return Session["group_names"].ToString().IndexOf(group_name) > 0;
			}catch
			{
				return false;
			}
		}


		public string appHomeUrl()
		{
			string fullUrl = Request.Url.ToString();
			if(fullUrl.IndexOf("/")>0)
			{
				return fullUrl.Substring(0, fullUrl.LastIndexOf("/")+1);
			}
			else
				return "";
		}
		public string navigateUrl(int pageId)
		{
			return appHomeUrl()+ "Default.aspx?pageId="+pageId.ToString();
		}
		public string navigateUrl(int pageId,string tenThamSo, string giaTriThamSo)
		{
			return appHomeUrl()+ "Default.aspx?pageId="+pageId.ToString()+"&"+tenThamSo+"="+giaTriThamSo;
		}
		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);
			if (userName==string.Empty)
			{
				//Response.Redirect(TTQTCommon.TTQTWebHomePath +"/Administration/Login.aspx");
                Response.Redirect("SessionTimeOut.html");
			}
			string fullUrl = Request.Url.ToString();
			string aspxPageName = fullUrl.Substring(fullUrl.LastIndexOf("/")+1, fullUrl.Length - fullUrl.LastIndexOf("/")-1);
			if(aspxPageName.IndexOf("?")>0)
			{
				aspxPageName = aspxPageName.Substring(0, aspxPageName.IndexOf("?"));
			}
			if(isSuperAdmin.Equals("0") && aspxPageName.IndexOf("Default")<0 && aspxPageName.IndexOf("?")<0)
			{
                int r = DataAccessObject.instance().checkPermission(TTQTCommon.ToInt32(userId, 0), aspxPageName);
			    if(r==-2)
			    {
                    Response.Redirect("LostConnection.html");
			    }
				if(DataAccessObject.instance().checkPermission(TTQTCommon.ToInt32(userId,0),aspxPageName) <=0)
				{
					Response.Redirect("AccessDenied.html");
				}
			}
		}
	}
}
